package com.kumarn.android.drovoice;

import com.kumarn.android.drovoice.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends Activity {

	private ToggleButton serviceToggleButton;
	private ToggleButton lowBatteryToggleButton;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		serviceToggleButton = (ToggleButton) findViewById(R.id.service_toggle);
		serviceToggleButton.setOnCheckedChangeListener(_serviceToggleListener);

		lowBatteryToggleButton = (ToggleButton) findViewById(R.id.low_battery_toggle);
		lowBatteryToggleButton.setOnCheckedChangeListener(_lowBatteryToggleListener);

	}



	OnCheckedChangeListener _serviceToggleListener = new OnCheckedChangeListener() {
		public void onCheckedChanged(CompoundButton compoundbutton, boolean flag) {
			if (flag) {

				/* サービス開始 */
				startService(new Intent(MainActivity.this, VolVoiceService.class));
				Toast.makeText(MainActivity.this, "常駐開始", Toast.LENGTH_LONG).show();

			} else {
				if(VolVoiceService._BatteryBroadcastReceiver.getDebugUnregister()){
					/* レシーバ解除 */
					VolVoiceService._BatteryHandler_unreg.handleMessage(null);
				}
				if(VolVoiceService._ScreenBroadcastReceiver.getDebugUnregister()){
					/* レシーバ解除 */
					VolVoiceService._ScreenHandler_unreg.handleMessage(null);

				}

				/* サービス停止 */
				stopService(new Intent(MainActivity.this, VolVoiceService.class));
				Toast.makeText(MainActivity.this, "常駐解除", Toast.LENGTH_LONG).show();
			}
		}
	};

	OnCheckedChangeListener _lowBatteryToggleListener = new OnCheckedChangeListener() {
		public void onCheckedChanged(CompoundButton compoundbutton, boolean flag) {
			if (flag) {
				VolVoiceService._BatteryHandler_reg.handleMessage(null);
			} else {
				if(VolVoiceService._BatteryBroadcastReceiver.getDebugUnregister()){
					/* レシーバ解除 */
					VolVoiceService._BatteryHandler_unreg.handleMessage(null);
				}
			}
		}
	};

	private Menu menu;
	private static final int MENU1 = 1;
	private static final int MENU2 = 2;

	@Override
	public boolean onCreateOptionsMenu(Menu menu1) {
		menu = menu1;
		menu.add(Menu.NONE, MENU1, Menu.NONE, "start");
		menu.add(Menu.NONE, MENU2, Menu.NONE, "stop");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		boolean ret = true;
		switch (item.getItemId()) {
		default:
			ret = super.onOptionsItemSelected(item);
			break;
		case MENU1:

			/* サービス開始 */
			startService(new Intent(MainActivity.this, VolVoiceService.class));

			Toast.makeText(MainActivity.this, "常駐開始", Toast.LENGTH_LONG).show();
			break;
		case MENU2:

			/* レシーバ解除 */
			VolVoiceService._BatteryHandler_unreg.handleMessage(null);
			VolVoiceService._ScreenHandler_unreg.handleMessage(null);

			/* サービス停止 */
			stopService(new Intent(MainActivity.this, VolVoiceService.class));

			Toast.makeText(MainActivity.this, "常駐解除", Toast.LENGTH_LONG).show();
			break;
		}
		return ret;
	}
}